abstract class Command {
  abstract execute(a: number, b: number): number;
}

class Add extends Command {
  execute(a: number, b: number) {
    return a + b;
  }
}

class Sub extends Command {
  execute(a: number, b: number) {
    return a - b;
  }
}

class Mul extends Command {
  execute(a: number, b: number) {
    return a * b;
  }
}

class Div extends Command {
  execute(a: number, b: number) {
    return a / b;
  }
}

/**
 * 策略模式 计算器类
 * @example
 */
export class Calculator {
  private strategy: Command | null = null;
  setStrategy(strategy: Command) {
    this.strategy = strategy;
  }
  calculate(a: number, b: number) {
    return this.strategy?.execute(a, b);
  }
}

//#region example
// const calculator = new Calculator();
// calculator.setStrategy(new Add());
// console.log(calculator.calculate(1, 2)); // 3

// calculator.setStrategy(new Sub());
// console.log(calculator.calculate(1, 2)); // -1

// calculator.setStrategy(new Mul());
// console.log(calculator.calculate(1, 2)); // 2

// calculator.setStrategy(new Div());
// console.log(calculator.calculate(1, 2)); // 0.5
//#endregion
